/**
 * Created by DukeCrushIt on 2015/8/27.
 */
class MissionItemRender extends eui.ItemRenderer{
    public constructor(){
        super();
        this.skinName = "pet.MissionItemRenderSkin";
    }

    public onGet(evt:egret.TouchEvent){
        SoundMgr.getInstance().playEffect(SoundMgr.BTN);
        window["__missionAward"](this.data);
    }

    public img_get:eui.Image;
    public img_progressing:eui.Image;
    public lab_content:eui.Label;
    public group_qty_0:eui.BitmapLabel;
    public ico_prize_0:eui.Image;
    public group_qty_1:eui.BitmapLabel;
    public ico_prize_1:eui.Image;
    //public img_complete:eui.Image;
    public partAdded(name:string, instance:any){
        super.partAdded(name, instance);
        if(name == "img_get"){
            this.img_get = instance;
            this.img_get.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onGet, this);
        }else if(name == "img_progressing"){
            this.img_progressing = instance;
        }else if(name == "group_qty_0"){
            this.group_qty_0 = instance;
        }else if(name == "ico_prize_0"){
            this.ico_prize_0 = instance;
        }else if(name == "group_qty_1"){
            this.group_qty_1 = instance;
        }else if(name == "ico_prize_1"){
            this.ico_prize_1 = instance;
        }
        //else if(name == "img_complete"){
        //    this.img_complete = instance;
        //}
    }

    public dataChanged(){
        super.dataChanged();
        this.reset();
        var mission:MissionInfo = Model.missionModels[this.data] || Model.achieveModels[this.data];
        var hairPrize:number = mission.dogHair || mission.catHair;
        var had = false;
        if(hairPrize != 0){
            this.ico_prize_0.source = mission.dogHair == 0 ? "img_money_cat" : "img_money_dog";
            this.ico_prize_0.visible = this.group_qty_0.visible = true;
            // this.ico_prize_0.commitProperties();
            this.group_qty_0.text = GetNumString(hairPrize,true);
            had = true;
        }
        if(mission.diamond!=0){
            if( !had ){
                this.ico_prize_0.source = "img_diamond";
                this.group_qty_0.text = mission.diamond+"";
                this.ico_prize_0.visible = this.group_qty_0.visible = true;
            }else{
                this.ico_prize_1.source = "img_diamond";
                this.group_qty_1.text = mission.diamond+"";
                this.ico_prize_1.visible = this.group_qty_1.visible = true;
            }
            had = true;
        }
        if( mission.props.length != 0){
            var prop = mission.props[0];
            if( !had){
                this.ico_prize_0.source = GameConst.Res_Root+"/resource/assets/items/"+prop[0]+".png?v="+GameConst.Version;
                this.group_qty_0.text =prop[1]+"";
                this.ico_prize_0.visible = this.group_qty_0.visible = true;
            }else{
                this.ico_prize_1.source = GameConst.Res_Root+"/resource/assets/items/"+prop[0]+".png?v="+GameConst.Version;
                this.group_qty_1.text =prop[1]+"";
                this.ico_prize_1.visible = this.group_qty_1.visible = true;
            }
        }
        this.lab_content.text = mission.name + "("+ mission.currStep+"/"+mission.steps+")";
        this.img_get.visible = mission.status  == 1;
        this.img_progressing.visible = mission.status == 0;
        //this.img_complete.visible = mission.status == 2;
    }

    private reset(){
        this.ico_prize_0.visible = false;
        this.group_qty_0.visible = false;
        this.ico_prize_1.visible = false;
        this.group_qty_1.visible = false;
    }
}